package _07xpath属性查找;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.Node;
import org.dom4j.io.SAXReader;

import java.util.List;

public class Demo7 {
    public static void main(String[] args) throws DocumentException {
        SAXReader reader = new SAXReader();
        Document document = reader.read("Day_14\\xpath_contact.xml");

        //查找所有具备id属性的元素
        Element rool = document.getRootElement();
        List<Node> nodes1 = rool.selectNodes("//*[@id]");
        for (Node n : nodes1) {
          Element e = (Element)n;
            System.out.println(e.attributeValue("vip"));

        }

        //查找所有具备id属性的contact元素
        List<Node> nodes2 = rool.selectNodes("//contact[@id]");
        for (Node n : nodes2) {
            Element e = (Element)n;
            System.out.println(e.attributeValue("id"));

        }

        //打印所有vip属性值为true的contact元素里面的name值的文本值
        List<Node> nodes3 = rool.selectNodes("//contact[@vip='true']/name");
        for (Node n : nodes3) {

            System.out.println(n.getText());

        }

        //获取文本值为“李四”的name标签。获取id的值
        List<Node> nodes4 = rool.selectNodes("//name[text()='李四']");
        for (Node n : nodes4) {
            Element e = (Element)n;
            System.out.println(e.attributeValue("id"));
        }






    }
}
